_implementation.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. """The match_hostname() function from Python 3.3.3, essential when using SSL."""
  2. # Note: This file is under the PSF license as the code comes from the python
  3. # stdlib. http://docs.python.org/3/license.html
  4. import re
  5. import sys
  6. # ipaddress has been backported to 2.6+ in pypi. If it is installed on the
  7. # system, use it to handle IPAddress ServerAltnames (this was added in
  8. # python-3.5) otherwise only do DNS matching. This allows
  9. # backports.ssl_match_hostname to continue to be used in Python 2.7.
  10. try:
  11. from pip._vendor import ipaddress
  12. except ImportError:
  13. ipaddress = None
  14. __version__ = "3.5.0.1"
  15. class CertificateError(ValueError):
  16. pass
  17. def _dnsname_match(dn, hostname, max_wildcards=1):
  18. """Matching according to RFC 6125, section 6.4.3
  19. http://tools.ietf.org/html/rfc6125#section-6.4.3
  20. """
  21. pats = []
  22. if not dn:
  23. return False
  24. # Ported from python3-syntax:
  25. # leftmost, *remainder = dn.split(r'.')
  26. parts = dn.split(r".")
  27. leftmost = parts[0]
  28. remainder = parts[1:]
  29. wildcards = leftmost.count("*")
  30. if wildcards > max_wildcards:
  31. # Issue #17980: avoid denials of service by refusing more
  32. # than one wildcard per fragment. A survey of established
  33. # policy among SSL implementations showed it to be a
  34. # reasonable choice.
  35. raise CertificateError(
  36. "too many wildcards in certificate DNS name: " + repr(dn)
  37. )
  38. # speed up common case w/o wildcards
  39. if not wildcards:
  40. return dn.lower() == hostname.lower()
  41. # RFC 6125, section 6.4.3, subitem 1.
  42. # The client SHOULD NOT attempt to match a presented identifier in which
  43. # the wildcard character comprises a label other than the left-most label.
  44. if leftmost == "*":
  45. # When '*' is a fragment by itself, it matches a non-empty dotless
  46. # fragment.
  47. pats.append("[^.]+")
  48. elif leftmost.startswith("xn--") or hostname.startswith("xn--"):
  49. # RFC 6125, section 6.4.3, subitem 3.
  50. # The client SHOULD NOT attempt to match a presented identifier
  51. # where the wildcard character is embedded within an A-label or
  52. # U-label of an internationalized domain name.
  53. pats.append(re.escape(leftmost))
  54. else:
  55. # Otherwise, '*' matches any dotless string, e.g. www*
  56. pats.append(re.escape(leftmost).replace(r"\*", "[^.]*"))
  57. # add the remaining fragments, ignore any wildcards
  58. for frag in remainder:
  59. pats.append(re.escape(frag))
  60. pat = re.compile(r"\A" + r"\.".join(pats) + r"\Z", re.IGNORECASE)
  61. return pat.match(hostname)
  62. def _to_unicode(obj):
  63. if isinstance(obj, str) and sys.version_info < (3,):
  64. obj = unicode(obj, encoding="ascii", errors="strict")
  65. return obj
  66. def _ipaddress_match(ipname, host_ip):
  67. """Exact matching of IP addresses.
  68. RFC 6125 explicitly doesn't define an algorithm for this
  69. (section 1.7.2 - "Out of Scope").
  70. """
  71. # OpenSSL may add a trailing newline to a subjectAltName's IP address
  72. # Divergence from upstream: ipaddress can't handle byte str
  73. ip = ipaddress.ip_address(_to_unicode(ipname).rstrip())
  74. return ip == host_ip
  75. def match_hostname(cert, hostname):
  76. """Verify that *cert* (in decoded format as returned by
  77. SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 and RFC 6125
  78. rules are followed, but IP addresses are not accepted for *hostname*.
  79. CertificateError is raised on failure. On success, the function
  80. returns nothing.
  81. """
  82. if not cert:
  83. raise ValueError(
  84. "empty or no certificate, match_hostname needs a "
  85. "SSL socket or SSL context with either "
  86. "CERT_OPTIONAL or CERT_REQUIRED"
  87. )
  88. try:
  89. # Divergence from upstream: ipaddress can't handle byte str
  90. host_ip = ipaddress.ip_address(_to_unicode(hostname))
  91. except ValueError:
  92. # Not an IP address (common case)
  93. host_ip = None
  94. except UnicodeError:
  95. # Divergence from upstream: Have to deal with ipaddress not taking
  96. # byte strings. addresses should be all ascii, so we consider it not
  97. # an ipaddress in this case
  98. host_ip = None
  99. except AttributeError:
  100. # Divergence from upstream: Make ipaddress library optional
  101. if ipaddress is None:
  102. host_ip = None
  103. else:
  104. raise
  105. dnsnames = []
  106. san = cert.get("subjectAltName", ())
  107. for key, value in san:
  108. if key == "DNS":
  109. if host_ip is None and _dnsname_match(value, hostname):
  110. return
  111. dnsnames.append(value)
  112. elif key == "IP Address":
  113. if host_ip is not None and _ipaddress_match(value, host_ip):
  114. return
  115. dnsnames.append(value)
  116. if not dnsnames:
  117. # The subject is only checked when there is no dNSName entry
  118. # in subjectAltName
  119. for sub in cert.get("subject", ()):
  120. for key, value in sub:
  121. # XXX according to RFC 2818, the most specific Common Name
  122. # must be used.
  123. if key == "commonName":
  124. if _dnsname_match(value, hostname):
  125. return
  126. dnsnames.append(value)
  127. if len(dnsnames) > 1:
  128. raise CertificateError(
  129. "hostname %r "
  130. "doesn't match either of %s" % (hostname, ", ".join(map(repr, dnsnames)))
  131. )
  132. elif len(dnsnames) == 1:
  133. raise CertificateError("hostname %r doesn't match %r" % (hostname, dnsnames[0]))
  134. else:
  135. raise CertificateError(
  136. "no appropriate commonName or subjectAltName fields were found"
  137. )